import axios from 'axios'
// import store from '@store'

const tokenKey = 'x-auth-token'

const BASE_URL = {
    development: 'http://172.31.240.125:82/',
    test: 'http://172.31.240.125:82/',
    production: 'http://172.31.20.141:81/'
}


export const uploadFile = (file, url) => {
    const param = new FormData()
    param.append("file", file)

    const config = {
        headers: { "Content-Type": "multipart/form-data" }
    }

    return axios.post(BASE_URL[process.env.NODE_ENV] + url, param, config).then(res => res)
}

export const createService = (baseUrl) => {
    const service = axios.create({
        baseURL: baseUrl[process.env.NODE_ENV],
        headers: {
            'Content-Type': 'application/json;charset=UTF-8'
        },
        timeout: 20000
    })

    service.interceptors.request.use(
        config => {
            // if (store.getters.token) {
            //     config.headers[tokenKey] = store.getters.token
            // }

            return config
        },
        error => {
            Promise.reject(error)
        }
    )

    service.interceptors.response.use(
        response => {
            if (response.status === 200) {
                if (response.data && response.data.status === true) {
                    return Promise.resolve(response.data.data)
                }
                return Promise.reject({ message: response.data.msg, data: response.data })
            }
            return Promise.reject({ message: '出错了' })
        },
        error => {
            return Promise.reject(error)
        }
    )

    return service
}

export default createService(BASE_URL)